//
//  HLSpaceSystermViewController.m
//  HLAerospace
//
//  Created by hulian-iOS on 17/9/28.
//  Copyright © 2017年 李斌. All rights reserved.
//

#import "HLSpaceSystermViewController.h"
#import "MBProgressHUD+HL.h"
#import "HttpRequestManager.h"
#import <MJExtension/MJExtension.h>
#import "HLUserInfo.h"
#import "HLFoldList.h"
#import "HLSubSystem.h"
#import "HLSubDetail.h"
#import "HLFoldContent.h"
#import "HLFoldSectionView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "HLFoldCourseCell.h"
#import "HLMoreCourse.h"
#import "HLCourseDetailViewController.h"
#import "HLArticleViewController.h"
#import "HLFoldCourse.h"

#define kTableViewRowHeight IS_IPHONE_5 ? 44 : 50

@interface HLSpaceSystermViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_requestData;
    NSInteger _currentIndex;
}
@property(nonatomic,strong)NSMutableDictionary *showDic;
@property(nonatomic,strong)UITableView *tableView;

/** 数据源  */
@property (nonatomic ,strong) NSMutableArray *dataSource;

@end

@implementation HLSpaceSystermViewController

- (NSMutableDictionary *)showDic
{
    if (!_showDic) {
        _showDic = [NSMutableDictionary dictionary];
    }
    return _showDic;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadRequest];
    
    [self setUI];
}

- (void)setUI
{
    self.navigationItem.title = @"航天课件";
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = HLRGB(240, 240, 240);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[HLFoldCourseCell class] forCellReuseIdentifier:@"HLFoldCourseCell"];

}

- (void)loadRequest
{
    [MBProgressHUD show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"sign"] = [kHLRequestGetSubsystemlist signStr];
    params[@"system_id"] = @"1";
    __weak typeof(self) weakSelf = self;
    [[HTTPRequestManager defaultManager] HTTPGetRequestWithApiName:kHLRequestGetSubsystemlist parameters:params success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject){
        [MBProgressHUD hideHUD];
        if ([responseObject[@"code"] integerValue] == 200) {
            __strong typeof(weakSelf) strongSelf = weakSelf;
            NSMutableArray *tempDatas = [NSMutableArray array];
            tempDatas = [HLSubSystem mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            _requestData = tempDatas;
//            self.dataSource = tempDatas;
//            [strongSelf _dealWithDatas:tempDatas];
            [strongSelf.tableView reloadData];
            
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showErrorMessage:@"网络超时"];
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    HLSubSystem *sub = _requestData[section];
    return sub.subsystem_list.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _requestData.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLFoldCourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HLFoldCourseCell"];
    HLSubSystem *sub = _requestData[indexPath.section];
    HLSubDetail *deta = sub.subsystem_list[indexPath.row];
    cell.subDetail = deta;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *key = [NSString stringWithFormat:@"%ld",indexPath.section];
    
//    if (self.showDic[[NSString stringWithFormat:@"%zd",indexPath.section]]) {
////        HLFoldCourse *couse = self.dataSource[indexPath.section][indexPath.row];
//        IS_IPHONE_5 ? 44 : 50;
//    }
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HLFoldSectionView *view = [[HLFoldSectionView alloc] init];
    view.frame = CGRectMake(0, 0, ScreenWidth, kTableViewRowHeight);
    view.subSystem = _requestData[section];
    view.tag = section;
    view.isUnFold = self.showDic[[NSString stringWithFormat:@"%ld",section]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    [view addGestureRecognizer:tap];
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLSubSystem *sub = _requestData[indexPath.section];
    HLSubDetail *deta = sub.subsystem_list[indexPath.row];
    HLFoldCourse *couse = [HLFoldCourse new];
    couse.wkid = [NSString stringWithFormat:@"%zd",deta.wkid];
    if (deta.type == 1) {
        HLCourseDetailViewController *couseDetail = [HLCourseDetailViewController new];
        couseDetail.foldCourse = couse;
        couseDetail.moreCourse = self.course;
        [self.navigationController pushViewController:couseDetail animated:YES];
    }
    if (deta.type == 2) {
        HLArticleViewController *article = [HLArticleViewController new];
        article.foldCourse = couse;
        article.moreCourse = self.course;
        [self.navigationController pushViewController:article animated:YES];
    }
}

#pragma mark - HLFoldSectionViewDelegate

- (void)tap:(UITapGestureRecognizer *)tapGestureRecognizer
{
    NSInteger index = tapGestureRecognizer.view.tag;
    _currentIndex = index;
    NSString *key = [NSString stringWithFormat:@"%ld",index];
    if (![_showDic objectForKey:key]) {
        [_showDic setObject:@(YES) forKey:key];
    }else{
        [_showDic removeObjectForKey:key];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
}



#pragma mark - Private

/**
 处理服务器返回的数据
 
 @param temp 服务器返回的数据
 */
//- (void)_dealWithDatas:(NSMutableArray *)temp
//{
//    for (HLFoldList *list in temp) {
//        NSMutableArray *tempArray = [NSMutableArray array];
//        for (HLFoldContent *content in list.list) {
//            /**** 是否需要标题的cell ****/
//            if (content.content.count == 0) {
//                continue;
//            }
//            HLFoldCourse *couese = content.content[0];
//            couese.headTitleText = content.list_name;
//            couese.isFirst = YES;
//            [tempArray addObjectsFromArray:content.content];
//        }
//        HLFoldCourse *course = tempArray.lastObject;
//        course.isHeadLine = YES;
//        [self.dataSource addObject:tempArray];
//    }
//    
//}


@end



